/**
 * Created by ZFL on 2017/12/22.
 */
var PageInit = function () {
    $(".but-login").click(function () {
        if ($("#tel").val() == "") {
            YDUI.dialog.toast("先填写手机号码",1000);
        } else if ($("#tel").val().length != 11) {
            YDUI.dialog.toast("手机号码格式不正确",1000);
        } else if ($("#code-id").val() == "") {
            YDUI.dialog.toast("请填写验证码",1000);
        } else if ($("#code-id").val().length < 4) {
            YDUI.dialog.toast("验证码长度有误");
        } else if ($("#pwd").val() == "") {
            YDUI.dialog.toast("请输入密码",1000);
        } else if ($("#pwd").val().length < 6) {
            YDUI.dialog.toast("密码长度不能小于6位",1000);
        } else {
            resetpwd();
        }
    });
    //点击
    $(".but").click(function () {
        sendCode();
    });
};

// 重置密码
function resetpwd() {
    if ($.getStorage("xm_uData")) {
        $.delStorage("xm_uData");
    }
    $.ajaxHandler({
        model: function () {
            var obj = {};
            obj.code = $("#code-id").val();
            obj.newPass =''+hex_md5($("#pwd").val())+'';
            obj.userName = $("#tel").val();
            obj.type =2; //获取微信端
            console.log(obj)
            return obj;
        },
        visibleTime: 500,
        dataType: "text",
        api: "yg_baijiaapp/user/updateUserPasswordByUserName",
        success: function (result) {
            //console.log(result);
            if (result.code == 10000) {
                YDUI.dialog.toast("修改成功",'success',1000);
                setTimeout(function () {
                    window.location.href='user_login.html';
                },200);
            }else {
                YDUI.dialog.toast(result.message,'error',1000);
            }
        }
    });
}
//发送短信验证码
function sendCode() {
    if ($("#tel").val() == ""){
        YDUI.dialog.toast("请先输入手机号码",1000);
    } else if ($("#tel").val().length != 11) {
        YDUI.dialog.toast("手机号码格式不正确",1000);
    } else {
        $("#send-code-id").attr("disabled","disabled");
        $.ajaxHandler({
                model: function () {
                    var obj = {};
                    obj.mobile = $("#tel").val();
                    obj.type = 1;
                    return obj;
                },
                visibleTime: 100,
                dataType: "json",
                api: 'yg_baijiaapp/CheckCode/sendVerificationCode',
                success: function (result) {
                    //console.log(result);
                    if (result.code == 10000) {
                        YDUI.dialog.toast("短信发送成功",'success',1000);
                        SendTime("#send-code-id");
                    } else if (result.code == 20112) {
                        YDUI.dialog.toast('短信发送失败','error',1000);
                    } else if (result.code == 20000) {
                        YDUI.dialog.toast('请求失败','error',1000);
                    } else if (result.code == 29901) {
                        YDUI.dialog.toast('无效参数','error',1000);
                    } else {
                        YDUI.dialog.toast(result.message,"error",1000);
                    }

                }
            }
        );
    }
}

var wait = 60;//短信时间
function SendTime(parameter) {
    var Send = $("" + parameter + "");
    if (wait == 0) {
        Send.html("| 点击获取验证码");
        wait = 60;
        $("#send-code-id").removeAttr("disabled");
    } else {
        Send.html("重新发送(" + wait + ")s");
        wait--;
        setTimeout(function () {
            SendTime(parameter);
        }, 1000);
    }
}